package com.gukun.springboot.tdengine.util;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
public class ZipGenerator {
    public static byte[] generateZip() throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(byteArrayOutputStream)) {
            // 设置编码
            zipOutputStream.setEncoding("UTF-8");

            // 添加第一个文件
            String json1 = "{\"name\":\"Alice\",\"age\":25}";
            ZipArchiveEntry entry1 = new ZipArchiveEntry("data1.json");
            zipOutputStream.putArchiveEntry(entry1);
            zipOutputStream.write(json1.getBytes(StandardCharsets.UTF_8));
            zipOutputStream.closeArchiveEntry();

            // 添加第二个文件
            String json2 = "{\"name\":\"Bob\",\"age\":30}";
            ZipArchiveEntry entry2 = new ZipArchiveEntry("data2.json");
            zipOutputStream.putArchiveEntry(entry2);
            zipOutputStream.write(json2.getBytes(StandardCharsets.UTF_8));
            zipOutputStream.closeArchiveEntry();
        }
        FileOutputStream fos = new FileOutputStream("D:\\data2.zip");
        fos.write(byteArrayOutputStream.toByteArray());
        fos.close();
        return byteArrayOutputStream.toByteArray();
    }

    public static void main(String[] args) throws Exception {
        byte[] zipBytes = ZipGenerator.generateZip();
        System.out.println("Generated ZIP file size: " + zipBytes.length);
    }
}
